package com.jrowny.controls
{
	
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	import mx.containers.Box;
	import mx.core.INavigatorContent;
	import mx.core.UIComponent;
	import mx.managers.CursorManager;
	import mx.managers.CursorManagerPriority;
	
	[Bindable]
	public class SpriteElement extends UIComponent
	{
		
		public var cursorId:int;
		
		private var _isClass:Boolean;
		private var _elmName:String;
		private var _selected:Boolean;
		private var _tag:String;
		private var _pseudo:String = ""; //initialized
		
		private var selectedChanged:Boolean;
		
		private var br_dragger:Dragger;
		private var tr_dragger:Dragger;
		private var tl_dragger:Dragger;
		private var bl_dragger:Dragger;
		private var tc_dragger:Dragger;
		private var bc_dragger:Dragger;
		private var rc_dragger:Dragger;
		private var lc_dragger:Dragger;
		
		private var selectionArea:Box;
		
		//used for resizing
		public var isResizing:Boolean = false;
		public var rect:Rectangle;
		
		
		private static const DRAGGER_SIZE:int = 4;
		
		public function SpriteElement(elmName:String,isClass:Boolean = false,tag:String="div")
		{
			_elmName = elmName;
			_isClass = isClass; 
			_tag = tag;
			selectionArea = new Box();
			createDraggers();			
			this.addChild(selectionArea);
			this.addEventListener(MouseEvent.MOUSE_OVER,function():void{ cursorId = CursorManager.setCursor(Cursors.MOVE,CursorManagerPriority.HIGH,-16,-16); });
			this.addEventListener(MouseEvent.MOUSE_OUT,function():void{ CursorManager.removeCursor(cursorId); });
			
		}
		
		
		
		private function createDraggers():void{
			//bottom right dragger
			br_dragger = new Dragger(Dragger.BOTTOM_RIGHT,Cursors.DIAGONAL_ONE);
			br_dragger.styleName="dragger";
			br_dragger.width=DRAGGER_SIZE;
			br_dragger.height=DRAGGER_SIZE;	
			//top right dragger
			tr_dragger = new Dragger(Dragger.TOP_RIGHT,Cursors.DIAGONAL_TWO);
			tr_dragger.styleName="dragger";
			tr_dragger.width=DRAGGER_SIZE;
			tr_dragger.height=DRAGGER_SIZE;
			tr_dragger.y = -DRAGGER_SIZE;
			//bottom left dragger
			bl_dragger = new Dragger(Dragger.BOTTOM_LEFT,Cursors.DIAGONAL_TWO);
			bl_dragger.styleName="dragger";
			bl_dragger.width=DRAGGER_SIZE;
			bl_dragger.height=DRAGGER_SIZE;		
			bl_dragger.x = -DRAGGER_SIZE;	
			//top left dragger
			tl_dragger = new Dragger(Dragger.TOP_LEFT,Cursors.DIAGONAL_ONE);
			tl_dragger.styleName="dragger";
			tl_dragger.width=DRAGGER_SIZE;
			tl_dragger.height=DRAGGER_SIZE;
			tl_dragger.y = -DRAGGER_SIZE;
			tl_dragger.x = -DRAGGER_SIZE;
			//bottom center dragger
			bc_dragger = new Dragger(Dragger.BOTTOM_CENTER,Cursors.VERTICAL);
			bc_dragger.styleName="dragger";
			bc_dragger.width=DRAGGER_SIZE;
			bc_dragger.height=DRAGGER_SIZE;	
			//top center dragger
			tc_dragger = new Dragger(Dragger.TOP_CENTER,Cursors.VERTICAL);
			tc_dragger.styleName="dragger";
			tc_dragger.width=DRAGGER_SIZE;
			tc_dragger.height=DRAGGER_SIZE;
			tc_dragger.y = -DRAGGER_SIZE;
			//center left dragger
			lc_dragger = new Dragger(Dragger.LEFT_CENTER,Cursors.HORIZONTAL);
			lc_dragger.styleName="dragger";
			lc_dragger.width=DRAGGER_SIZE;
			lc_dragger.height=DRAGGER_SIZE;			
			lc_dragger.x = -(DRAGGER_SIZE);
			//center right dragger
			rc_dragger = new Dragger(Dragger.RIGHT_CENTER,Cursors.HORIZONTAL);
			rc_dragger.styleName="dragger";
			rc_dragger.width=DRAGGER_SIZE;
			rc_dragger.height=DRAGGER_SIZE;
			
			this.addChild(br_dragger);
			this.addChild(tr_dragger);
			this.addChild(tl_dragger);
			this.addChild(bl_dragger);
			this.addChild(bc_dragger);
			this.addChild(tc_dragger);
			this.addChild(rc_dragger);
			this.addChild(lc_dragger);
		}
		
		override protected function createChildren():void{
			super.createChildren();
			
		}
		
			override protected function commitProperties():void{			
				if(selectedChanged){		
					if(_selected){
						selectionArea.styleName = "selected";
						br_dragger.styleName = "dragger";
						tr_dragger.styleName = "dragger";
						tl_dragger.styleName = "dragger";
						bl_dragger.styleName = "dragger";
						bc_dragger.styleName = "dragger";
						tc_dragger.styleName = "dragger";
						rc_dragger.styleName = "dragger";
						lc_dragger.styleName = "dragger";
					}else{
						selectionArea.styleName = "element";
						br_dragger.styleName = "draggerOff";
						tr_dragger.styleName = "draggerOff";
						tl_dragger.styleName = "draggerOff";
						bl_dragger.styleName = "draggerOff";
						bc_dragger.styleName = "draggerOff";
						tc_dragger.styleName = "draggerOff";
						rc_dragger.styleName = "draggerOff";
						lc_dragger.styleName = "draggerOff";
					}
					selectedChanged = false;
				}
				super.commitProperties();
			}
		
		public function get selected():Boolean{
			return _selected;
		}
		public function set selected(value:Boolean):void{
			if(_selected!=value){
				_selected = value;
				selectedChanged = true;
				invalidateProperties();
			}
		}
				
		override public function set x(value:Number):void{
			super.x = Math.round(value);
		}
		override public function set y(value:Number):void{
			super.y = Math.round(value);
		}
		override public function set width(value:Number):void{
			selectionArea.width = Math.round(value);
			br_dragger.x = value;
			tr_dragger.x = value;
			rc_dragger.x = value;
			tc_dragger.x = (value-(DRAGGER_SIZE/2))/2;
			bc_dragger.x = (value-(DRAGGER_SIZE/2))/2;
			super.width = Math.round(value);
		}
		override public function set height(value:Number):void{
			selectionArea.height = Math.round(value);;
			lc_dragger.y = (value-(DRAGGER_SIZE/2))/2;
			rc_dragger.y = (value-(DRAGGER_SIZE/2))/2;			
			br_dragger.y = value;
			bc_dragger.y = value;
			bl_dragger.y = value;
			super.height = Math.round(value);
		}
		
		public function get elmName():String{
			return _elmName;
		}
		public function set elmName(value:String):void{
			_elmName = value;
		}
		public function get isClass():Boolean{
			return _isClass;
		}
		public function set isClass(value:Boolean):void{
			_isClass = value;
		}
		public function get tag():String{
			return _tag;
		}		
		public function set tag(value:String):void{
			_tag = value;
		}

		public function set pseudo(value:String):void{
			_pseudo = value;
		}
		public function get pseudo():String{
			return _pseudo;
		}
		public function get baseSelector():String{
			if(isClass){
				return "." + elmName;
			}
			return "#" + elmName;			
		}
		public function get fullSelector():String{
			if(isClass){
				return "." + elmName + ":" + pseudo;
			}
			return "#" + elmName + ":" + pseudo;		
		}
		
	}
}